

public class MultipleTryCatch {
    public static void main(String[] args) {
        int[] numbers = { 10, 20, 30, 0, 40 };

        for (int i = 0; i < numbers.length; i++) {
            try {
                int result = divideNumbers(numbers[i], i);
                System.out.println("Result: " + result);
            } catch (ArithmeticException e) {
                System.out.println("Divide by zero error: " + e.getMessage());
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println("Array index out of bounds: " + e.getMessage());
            }
        }
    }

    static int divideNumbers(int dividend, int divisor) {
        return switch (divisor) {
            case 0 -> throw new RuntimeException("Cannot divide by zero.");
            case 1, 2 -> dividend / divisor;
            default -> throw new ArrayIndexOutOfBoundsException("Invalid index: " + divisor);
        };
    }
}

